import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';


import '../config/color_config.dart';
import '../data/bean/dictionary.dart';
import '../utils/common.dart';
import '../utils/dialog_ext.dart';

class StorageDialog extends StatefulWidget {
  final List<Dictionary>? dicList;
  final String? title;
  final Function(Dictionary? pickerdata)? confirm;

  const StorageDialog(
      {Key? key, this.title, required this.dicList, this.confirm})
      : super(key: key);

  @override
  State<StorageDialog> createState() => _StorageDialogState();
}

class _StorageDialogState extends State<StorageDialog> {
  PickerDataAdapter? adapter;
  List? tempData = [];
  Map<String?, Dictionary?> temp = {};

  _StorageDialogState() : super();

  @override
  void initState() {
    super.initState();
    widget.dicList?.forEach((element) {
      tempData?.add(element.name);
      temp[element.name] = element;
    });
    adapter ??= PickerDataAdapter(pickerData: tempData);
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      top: false,
      child: _body(context),
    );
  }

  _body(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        _title(context),
        _content(),
      ],
    );
  }

  _title(BuildContext context) {
    return SizedBox(
      height: 54.w,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          SizedBox(
            width: 16.w,
          ),
          GestureDetector(
            onTap: () {
              Navigator.pop(context);
            },
            child: Text(
              '取消',
              style: TextStyle(fontSize: 16.sp, color: Color(0xFF86909C)),
            ),
          ),
          Expanded(
            child: Text(
              widget.title ?? "",
              textAlign: TextAlign.center,
              style: TextStyle(
                color: ColorConfig.FF333333,
                fontSize: 16.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
          GestureDetector(
            onTap: () {
              Navigator.pop(context);
              var selectedValues = adapter?.getSelectedValues();
              if(isNotEmptyOrNull(selectedValues)){
                Dictionary? item = temp[selectedValues?[0]];
                widget.confirm?.call(item);
              }
            },
            child: Container(
              height: double.infinity,
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(horizontal: 15.w),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                  topRight: Radius.circular(16.r),
                ),
              ),
              child: Text(
                "确定",
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: ColorConfig.FF007AFF,
                  fontSize: 16.sp,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  _content() {
    return Container(
      child: Picker(
              hideHeader: true,
              height: 200.w,
              itemExtent: 40.w,
              selectionOverlay: Container(
                margin: EdgeInsets.symmetric(horizontal: 8.w),
                decoration: BoxDecoration(
                  color: ColorConfig.black.withOpacity(0.05),
                  borderRadius: BorderRadius.circular(10.r),
                ),
              ),
              textStyle: TextStyle(
                  color: ColorConfig.FF020306,
                  fontSize: 16.sp,
                  fontWeight: FontWeight.w400),
              selectedTextStyle: TextStyle(
                color: ColorConfig.FF333333,
                fontSize: 21.sp,
              ),
              adapter: adapter!,
              title: Text(""),
              onConfirm: (Picker picker, List value) {})
          .makePicker(),
    );
  }
}

void showBottomDialog(BuildContext context,
    {String? title,
    List<Dictionary>? dicList,
    Function(Dictionary? pickerdata)? confirm}) {
  showSheetDialog(
    context,
    StorageDialog(
      title: title,
      dicList: dicList,
      confirm: confirm,
    ),
  );
}
